APE=/sys/src/ape
< $APE/config
<plan9/buildinfo
archname = plan9_$objtype
privroot = /sys/lib/perl
privlib  = $privroot/$p9pvers
archroot = /$objtype/lib/perl
archlib  = $archroot/$p9pvers
sitelib  = $privlib/site_perl
sitearch = $archlib/site_perl
corelib  = $archlib/CORE
arpalib  = $corelib/arpa

pwd=`{pwd}
cpanlib=`{cd cpan; ls -ld * | grep '^d' | awk '{ print $10 }' | grep -v '^\.'}
icpanlib=`{echo $pwd^/cpan/^$cpanlib^/lib}
distlib=`{cd dist; ls -ld * | grep '^d' | awk '{ print $10 }' | grep -v '^\.'}
idistlib=`{echo $pwd^/dist/^$distlib $pwd^/dist/^$distlib^/lib }
extlib=`{cd ext; ls -ld * | grep '^d' | awk '{ print $10 }' | grep -v '^\.'}
iextlib=`{echo $pwd^/ext/^$extlib $pwd^/ext/^$extlib/lib }

buildlibs=`{echo -I$pwd^/^(lib archlib privlib) -I^$icpanlib -I^$idistlib -I^$iextlib }
miniperl=`{echo $pwd/miniperl $buildlibs }
perl=`{echo $pwd/perl $buildlibs }
xscmd=`{echo $pwd/dist/ExtUtils-ParseXS/lib/ExtUtils/xsubpp -noprototypes -typemap $pwd/dist/ExtUtils-ParseXS/t/typemap}
miniperlxs=`{echo $miniperl $xscmd}
perlxs=`{echo $perl $xscmd}

BASECFLAGS=-B -D_SUSV2_SOURCE -D_POSIX_SOURCE -D_BSD_EXTENSION
PERLCFLAGS =-DPERL_CORE $BASECFLAGS
CFLAGS=-DPERL_IS_MINIPERL $PERLCFLAGS

CCCMD = $CC  -c $CFLAGS
PERLCCCMD= $CC -c $PERLCFLAGS
XSCCCMD= $CC -c $BASECFLAGS

perllib = archlib/CORE/libperl.a

perlshr = archlib/CORE/libperlshr.a

installman1dir = /sys/man/1
installman3dir = /sys/man/2

podnames = perl perlbook perldata perldebtut perldiag perldsc perlform perlfunc perlipc perllexwarn perllol perlmod perlmodlib perlmodinstall perlnewmod perlop perlootut perlopentut perlpacktut perlpod perlport perlrequick perlretut perlref perlreftut perlrequick perlrun perlsec perlstyle perlsub perlsyn perltie perltrap perlutil perlunifaq perluniintro perlvar
faqpodnames = perlfaq perlfaq1 perlfaq2 perlfaq3 perlfaq4 perlfaq5 perlfaq6 perlfaq7 perlfaq8 perlfaq9
advpodnames = perlapi perlapio perlcall perlclib perlcompile perldebguts perldbmfilter perldebug perldelta perldiag perlebcdic perlembed perlfilter perlfork perlguts perlhack perlintern perliol perllocale perlnumber perlobj perlpodspec perlre perlthrtut perltodo perlunicode perlunicook perlxs perlxs perlxstut
archpodnames = perlaix perlamiga perlbeos perlbs2000 perlce perlcygwin perldgux perlfreebsd perlhpux perlhurd perlirix perlmacosx perlmpeix perlos2 perlos390 perlos400 perlplan9 perlqnx perlsolaris perltru64 perlvms perlvos perlwin32
histpodnames = perl5004delta perl5005delta perl561delta perl56delta perl570delta perl571delta perl572delta perl573delta perl58delta perlhist

libpods = ${podnames:%=pod/%.pod}

perlpods = $libpods

extensions = File::Glob IO Opcode DynaLoader Fcntl POSIX Cwd re B Devel::Peek attributes Hash::Util Hash::Util::FieldHash mro Storable Time::HiRes Unicode::Normalize List::Util
ext_xs = Glob.xs IO.xs Opcode.xs  dl_none.xs Fcntl.xs POSIX.xs Cwd.xs re.xs B.xs Peek.xs attributes.xs Util.xs FieldHash.xs mro.xs Storable.xs HiRes.xs Normalize.xs ListUtil.xs
ext_c = ${ext_xs:%.xs=%.c}
ext_obj = ${ext_xs:%.xs=%.$O} bsd_glob.$O poll.$O re_comp.$O re_exec.$O

# Socket requires a full version of Perl (one with IO to build).
more_extensions = Socket Data::Dumper
more_ext_xs = Socket.xs Dumper.xs
more_ext_c = ${more_ext_xs:%.xs=%.c}
more_ext_obj = ${more_ext_xs:%.xs=%.$O}

obj = av.$O caretx.$O deb.$O doio.$O doop.$O dquote.$O dump.$O globals.$O gv.$O hv.$O keywords.$O locale.$O malloc.$O mathoms.$O mg.$O mro_core.$O numeric.$O op.$O pad.$O perlapi.$O perlio.$O perly.$O pp.$O pp_ctl.$O pp_hot.$O pp_pack.$O pp_sort.$O pp_sys.$O reentr.$O regcomp.$O regexec.$O run.$O scope.$O sv.$O taint.$O time64.$O toke.$O universal.$O utf8.$O util.$O

OBJS = perl.$O plan9.$O $obj

# TODO: what's this used for?
testlist = `{cd t; echo base/*.t comp/*.t cmd/*.t run/*.t io/*.t re/*.t opbasic/*.t op/*.t uni/*.t perf/*.t}

install:V:	perl preplibrary 
		cp perl /$objtype/bin/perl-$p9pvers
		cp plan9/aperl /rc/bin/Perl-$p9pvers
		
		if (test ! -d $privroot) mkdir $privroot
		if (test ! -d $privlib) mkdir $privlib
		if (test ! -d $privlib/auto) mkdir $privlib/auto
		if (test ! -d $sitelib) mkdir $sitelib
		if (test ! -d $archroot) mkdir $archroot
		if (test ! -d $archlib) mkdir $archlib
		if (test ! -d $sitearch) mkdir $sitearch
		if (test ! -d $corelib) mkdir $corelib
		if (test ! -d $arpalib) mkdir $arpalib
		cp *.h plan9/*.h  $corelib
		cp plan9/arpa/*.h  $arpalib
		
		# Populate library directories
		{cd privlib ; tar c . } | {cd $privlib ; tar x }
		{cd lib ; tar c . } | {cd $privlib ; tar x }
		{cd archlib ; tar c . } | {cd $archlib ; tar x }
		# mk man # don't make man pages now--resolve multiversion
		
perl:	perlnosocket more_extensions
		$LD $PERLCFLAGS -o perl perlmain.$O perl.$O $perllib $perlshr $more_ext_obj

perlnosocket:		config.h miniperlmain.$O miniperl archlib/Config.pm perlmain.$O $perlshr
		$LD $PERLCFLAGS -o perl perlmain.$O perl.$O $perllib $perlshr
		
miniperl:	config.h $perllib miniperlmain.$O 
		$LD  $CFLAGS -o miniperl miniperlmain.$O  $perllib 

preplibrary:V:	miniperl archlib/Config.pm
			mkdir -p privlib/auto
			for (file in lib/*.pm lib/*/*.pm archlib/Config.pm) $miniperl -e 'use AutoSplit; autosplit(@ARGV)' $file privlib/auto
	
$perllib(%):N:	%
$perllib: ${OBJS:%=$perllib(%)}
		mkdir -p archlib/CORE
		ar rv $perllib $OBJS
		$RANLIB $perllib
			
miniperlmain.$O:	config.h uudmap.h
				$CCCMD miniperlmain.c

perlmain.$O:	config.h perlmain.c
			$PERLCCCMD perl.c
			$PERLCCCMD perlmain.c

malloc.$O: malloc.c
			# Does the PTRDIFF_MAX need to be set per architecture?
			$CCCMD -DPTRDIFF_MAX^'='^9223372036854775807L malloc.c

perlmain.c:	miniperl extensions archlib/Config.pm
			$miniperl -MExtUtils::Miniperl -e 'writemain(\q{perlmain.c}, @ARGV)' $extensions

config.h:		plan9/fndvers
			plan9/fndvers 
			mkdir -p archlib/CORE
			cp config.h archlib/CORE

$perlshr(%):N:	%
$perlshr:  ${ext_obj:%=$perlshr(%)}
		ar rv $perlshr $ext_obj
		$RANLIB $perlshr

uudmap.h:	generate_uudmap.c
			$CCCMD generate_uudmap.c
			# This is currently a show-stopper for cross-compilation: find the true $objtype by looking at kernel?
			$LD $CFLAGS -o generate_uudmap generate_uudmap.$O
			chmod +x generate_uudmap
			./generate_uudmap uudmap.h bitcount.h mg_data.h

extensions:V: miniperl archlib/Config.pm $ext_c dist/XSLoader/XSLoader.pm

more_extensions:V: perlnosocket $more_ext_c $more_ext_obj
			ext_obj=`{echo $ext_obj $more_ext_obj}

Socket.$O: Socket.c
			$XSCCCMD -I plan9 Socket.c

attributes.$O: attributes.c
			$XSCCCMD -I plan9 attributes.c

mro.$O: mro.c
			$XSCCCMD -I plan9 mro.c

B.$O: B.c
			$XSCCCMD -I plan9 B.c

Peek.$O: Peek.c
			$XSCCCMD -I plan9 Peek.c

Util.$O: Util.c
			$XSCCCMD -I plan9 Util.c

FieldHash.$O: FieldHash.c
			$XSCCCMD -I plan9 FieldHash.c

re.$O: re.c
			$XSCCCMD -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT -I plan9 re.c

re_comp.$O: re_comp.c
			$XSCCCMD -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT -I plan9 re_comp.c

re_exec.$O: re_exec.c
			$XSCCCMD -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT -I plan9 re_exec.c

Cwd.$O: Cwd.c IO.c
			cp dist/IO/ppport.h dist/PathTools
			$XSCCCMD -DDOUBLE_SLASHES_SPECIAL^'='^0 -I plan9 Cwd.c

IO.$O: extensions
			$XSCCCMD -I plan9 IO.c

poll.$O: extensions
			$XSCCCMD -I plan9 poll.c

POSIX.$O: extensions
			$XSCCCMD -I plan9 POSIX.c

Glob.$O: extensions
			$XSCCCMD -I plan9 Glob.c

bsd_glob.$O: extensions
			$XSCCCMD -I plan9 -I. bsd_glob.c

Normalize.$O:	extensions
			$XSCCCMD -I plan9 -I. Normalize.c

Dumper.$O:	Dumper.c
			$XSCCCMD -I plan9 -I. Dumper.c

HiRes.$O:	extensions
			$XSCCCMD -I plan9 -I. HiRes.c

Storable.$O:	extensions
			$XSCCCMD -I plan9 -I. Storable.c

ListUtil.$O:	extensions
			$XSCCCMD -I plan9 -I. ListUtil.c

Opcode.$O: extensions
			$XSCCCMD -I plan9 Opcode.c

Fcntl.$O: extensions
			$XSCCCMD -I plan9 Fcntl.c

dl_none.$O: extensions
			$XSCCCMD -I plan9 dl_none.c

Glob.c:		archlib/Config.pm	ext/File-Glob/Glob.xs
			cd ext/File-Glob
			$miniperl Makefile.PL # to write constants	
			cd $pwd
			$miniperlxs ext/File-Glob/Glob.xs > $target
			cp ext/File-Glob/bsd_glob.^(c h) $pwd
			mkdir -p archlib/File
			cp ext/File-Glob/Glob.pm archlib/File/Glob.pm

ppport.h:   archlib/Config.pm
			if (test ! -f /bin/sh) bind -a /386/bin/ape /bin
			# specify the list, otherwise suicide
			$miniperl mkppport --list mkppport.lst

Cwd.c:		archlib/Config.pm dist/PathTools/Cwd.xs
			$miniperlxs dist/PathTools/Cwd.xs > $target
			cp dist/PathTools/Cwd.pm archlib
			{ cd dist/PathTools/lib; tar c . } | { mkdir -p archlib; cd archlib; tar x }

IO.c:		archlib/Config.pm dist/IO/IO.xs ppport.h
			$miniperlxs dist/IO/IO.xs > $target
			cp dist/IO/poll.^(c h) $pwd
			cp dist/IO/IO.pm archlib
			{ cd dist/IO/lib; tar c . } | { mkdir -p archlib; cd archlib; tar x }

dist/XSLoader/XSLoader.pm:  archlib/Config.pm
			cd dist/XSLoader
			$miniperl XSLoader_pm.PL
			cp XSLoader.pm $pwd/archlib

ext/Errno/Errno.pm: perlnosocket	
			cd ext/Errno
			$perl Makefile.PL
			$perl Errno_pm.PL arch.txt
			cp Errno.pm $pwd/archlib
		
Socket.c:		perlnosocket cpan/Socket/Socket.xs ext/Errno/Errno.pm
			cd cpan/Socket
			$perl Makefile.PL # to write const-xs.inc
			cd $pwd
			$perlxs cpan/Socket/Socket.xs > $target
			cp cpan/Socket/Socket.pm archlib

uni.data:	archlib/Config.pm lib/unicore/mktables
			$miniperl lib/unicore/mktables -C lib/unicore -makelist -p

Normalize.c:		archlib/Config.pm dist/Unicode-Normalize/Normalize.xs uni.data
			$miniperl dist/Unicode-Normalize/mkheader
			$miniperlxs dist/Unicode-Normalize/Normalize.xs > $target
			mkdir -p archlib/Unicode
			cp dist/Unicode-Normalize/Normalize.pm archlib/Unicode

Dumper.c:		perlnosocket dist/Data-Dumper/Dumper.xs
			$perlxs dist/Data-Dumper/Dumper.xs > $target
			mkdir -p archlib/Data
			cp dist/Data-Dumper/Dumper.pm archlib/Data

HiRes.c:		archlib/Config.pm dist/Time-HiRes/HiRes.xs
			cd dist/Time-HiRes
			$miniperl Makefile.PL # to write const-xs.inc
			cd $pwd
			$miniperlxs dist/Time-HiRes/HiRes.xs > $target
			mkdir -p archlib/Time
			cp dist/Time-HiRes/HiRes.pm archlib/Time

ListUtil.c:		archlib/Config.pm cpan/Scalar-List-Utils/ListUtil.xs
			cp cpan/Scalar-List-Utils/multicall.h $pwd
			$miniperlxs cpan/Scalar-List-Utils/ListUtil.xs > $target
			{ cd cpan/Scalar-List-Utils/lib; tar c . } | { mkdir -p archlib; cd archlib; tar x }

Storable.c:		archlib/Config.pm dist/Storable/Storable.xs
			$miniperlxs dist/Storable/Storable.xs > $target
			cp dist/Storable/Storable.pm archlib

Opcode.c:		archlib/Config.pm ext/Opcode/Opcode.xs
			$miniperlxs ext/Opcode/Opcode.xs > $target
			cp ext/Opcode/^(Opcode ops)^.pm archlib

Fcntl.c:		archlib/Config.pm ext/Fcntl/Fcntl.xs
			cd ext/Fcntl
			$miniperl Makefile.PL # to write const-xs.inc
			cd $pwd
			$miniperlxs ext/Fcntl/Fcntl.xs > $target
			cp ext/Fcntl/Fcntl.pm archlib

POSIX.c:		archlib/Config.pm ext/POSIX/POSIX.xs
			cd ext/POSIX
			$miniperl Makefile.PL # to write const-xs.inc
			cd $pwd
			$miniperlxs ext/POSIX/POSIX.xs > $target
			cp ext/POSIX/lib/POSIX.pm archlib

B.c:		archlib/Config.pm ext/B/B.xs
			cd ext/B
			$miniperl Makefile.PL PERL_CORE^'='^1 # to write const-xs.inc
			cd $pwd
			$miniperlxs ext/B/B.xs > $target
			cp ext/B/^(B O)^.pm archlib

Peek.c:		archlib/Config.pm ext/Devel-Peek/Peek.xs
			$miniperlxs ext/Devel-Peek/Peek.xs > $target
			cp ext/Devel-Peek/Peek.pm archlib

attributes.c:	archlib/Config.pm ext/attributes/attributes.xs
			$miniperlxs ext/attributes/attributes.xs > $target
			cp ext/attributes/attributes.pm archlib

mro.c:	archlib/Config.pm ext/mro/mro.xs
			$miniperlxs ext/mro/mro.xs > $target
			cp ext/mro/mro.pm archlib

Util.c:	archlib/Config.pm ext/Hash-Util/Util.xs
			$miniperlxs ext/Hash-Util/Util.xs > $target
			{ cd ext/Hash-Util/lib; tar c . } | { cd archlib; tar x }

FieldHash.c:	archlib/Config.pm ext/Hash-Util-FieldHash/FieldHash.xs
			$miniperlxs ext/Hash-Util-FieldHash/FieldHash.xs > $target
			{ cd ext/Hash-Util-FieldHash/lib; tar c . } | { cd archlib; tar x }
	
re.c:	archlib/Config.pm ext/re/re.xs
			$miniperlxs ext/re/re.xs > $target
			cp ext/re/re_^(comp top)^.h $pwd
			cp ext/re/re.pm archlib

re_comp.c:	miniperl re.c regcomp.c
			cp regcomp.c $target

re_exec.c:	miniperl re.c regexec.c
			cp regexec.c $target

dl_none.c:	archlib/Config.pm ext/DynaLoader/dl_none.xs
			cd ext/DynaLoader;
			$miniperl DynaLoader_pm.PL
			cd $pwd
			$miniperlxs ext/DynaLoader/dl_none.xs > $target
			cp ext/DynaLoader/DynaLoader.pm archlib

preptest:Q: perl
			# Bind the crap out of everything, as different tests expect different INCs
			cp perl t/perl
			cd t
			mntpt=/$objtype/lib/perl
			if (test ! -d $mntpt ) {
				mkdir $mntpt
				rmdir=1
			}
			ramfs -m $mntpt
			mkdir -p $mntpt/5.32.0/site_perl
			echo -n Binding directories
			for (l in $pwd/lib $pwd/archlib $icpanlib $idistlib $iextlib ) {
				if (test -d $l ) {
					if (! ~ `{grep $l <{ns} >[1]/dev/null; echo $status} '') bind -a $l $mntpt/5.32.0/site_perl
					echo -n .
				}
			}
			bind $mntpt/5.32.0/site_perl $pwd/lib
			echo done
			echo Binding sh
			if (! ~ `{grep /$objtype/bin/ape <{ns} >[1]/dev/null; echo $status} '') bind -a /$objtype/bin/ape /bin

minitest:V: preptest
			cd t
			perl TEST $testlist
			unmount $pwd/lib
			unmount $mntpt
			if (~ $rmdir 1) rm $mntpt
			rm t/perl

test:V: preptest
			cd t
			perl TEST `{ ls */*.t | comm -23 - ../plan9/exclude }
			unmount $pwd/lib
			unmount $mntpt
			if (~ $rmdir 1) rm $mntpt
			rm t/perl

plan9.$O:	config.h plan9/plan9.c
			cp plan9/plan9.c plan9.c
			$CCCMD plan9.c

%.$O:	config.h %.c
		$CCCMD $stem.c

archlib/Config.pm:		config.sh
				$miniperl make_patchnum.pl
				$miniperl configpm
				cp lib/Config.pm archlib/Config.pm
				cp lib/Config_heavy.pl archlib/Config_heavy.pl
				cp lib/Config_git.pl archlib/Config_git.pl


config.sh:	miniperl
		miniperl plan9/genconfig.pl $extensions $more_extensions

installall:V:	
			for (386 68000 68020 arm arm amd64 mips power power64 sparc sparc64 spim) mk install

man:V:		$perlpods pod/pod2man.PL perl
			perl pod/pod2man.PL
			for (i in $podnames) pod/pod2man pod/$i.pod > $installman3dir/$i
			for (i in $faqpodnames) pod/pod2man pod/$i.pod > $installman3dir/$i
			for (i in $advpodnames) pod/pod2man pod/$i.pod > $installman3dir/$i
			for (i in $archpodnames) pod/pod2man pod/$i.pod > $installman3dir/$i
			for (i in $histpodnames) pod/pod2man pod/$i.pod > $installman3dir/$i
			
nuke clean:V:	
		rm -f *.$O config.sh $perllib config.h $perlshr perlmain.c perl miniperl git_version.h $ext_c $more_ext_c generate_uudmap uudmap.h bitcount.h mg_data.h plan9.c plan9ish.h math.h bsd_glob.^(c h) poll.^(c h) re_*.^(c h) uni.data multicall.h
		rm -rf privlib archlib

deleteman:V:
			rm -f $installman1dir/perl* $installman3dir/perl*
